package com.wx.controller.api;

import com.github.pagehelper.PageInfo;
import com.wx.common.CreateValidate;
import com.wx.common.PageParam;
import com.wx.common.WrapMapper;
import com.wx.common.Wrapper;
import com.wx.constants.Constants;
import com.wx.domain.dto.NurseDto;
import com.wx.entity.SysNurse;
import com.wx.entity.SysPatient;
import com.wx.entity.SysPatientFollow;
import com.wx.holder.WxUserHolder;
import com.wx.service.SysNurseService;
import com.wx.service.SysPatientFollowService;
import com.wx.service.SysPatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * <p>Title:      ApiNurseController. </p>
 * <p>Description  </p>
 * <p>Copyright: Copyright (c) 2016</p>
 * <p>Company:    医百科技有限公司 </p>
 *
 * @author         yangwensheng@100doc.com.cn
 * @since      2021-03-03 11:36:07
 */
@RestController
@RequestMapping(value = "/api/sysNurse", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(tags = "护士工作台(API)", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class ApiNurseController {

    @Resource
    private SysPatientService sysPatientService;

    @Resource
    private SysPatientFollowService sysPatientFollowService;

    @Resource
    private SysNurseService sysNurseService;

    @Resource
    private WxUserHolder wxUserHolder;


    @GetMapping("/getNurseInfoByOpenId")
    @ApiOperation(httpMethod = "GET", value = "根据openId查询护士信息")
    public Wrapper<?> getNurseInfoByOpenId() {
        WxOAuth2UserInfo userInfo = wxUserHolder.getUserInfo();
        SysNurse result = sysNurseService.selectByOpenId(userInfo.getOpenid());
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE,result);
    }


    @PostMapping("/generateSmsCode/{mobile}")
    @ApiOperation(value = "生成短信验证码")
    public Wrapper<?> generateSmsCode(@PathVariable("mobile") String mobile) {
        sysNurseService.generateSmsCode(mobile);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE);
    }


    @PostMapping("/checkValidateCode/{mobile}/{validateCode}")
    @ApiOperation(httpMethod = "POST", value = "校验护士的短信验证码是否正确")
    public Wrapper<?> checkValidateCode(@PathVariable("mobile") String mobile,@PathVariable("validateCode") String validateCode) {
        WxOAuth2UserInfo userInfo = wxUserHolder.getUserInfo();
        SysNurse result = sysNurseService.checkValidateCode(userInfo.getOpenid(),mobile,validateCode);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE,result);
    }


    @PostMapping("/nurseAuthentication")
    @ApiOperation(httpMethod = "POST", value = "护士进行实名认证")
    public Wrapper<?> nurseAuthentication(@ApiParam(name = "nurseDto", value = "nurseDto") @RequestBody @Validated NurseDto nurseDto) {
        WxOAuth2UserInfo userInfo = wxUserHolder.getUserInfo();
        nurseDto.setOpenId(userInfo.getOpenid());
        int result = sysNurseService.nurseAuthentication(nurseDto);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE,result);
    }



    @PostMapping("/updateNurse")
    @ApiOperation(httpMethod = "POST", value = "更新护士的认证状态")
    public Wrapper<?> updatePatient(@ApiParam(name = "sysNurse", value = "sysNurse") @RequestBody SysNurse sysNurse) {
        int result = sysNurseService.updateStatus(sysNurse);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE,result);
    }


    @PostMapping("/updateAgreement/{patientId}")
    @ApiOperation(httpMethod = "POST", value = "更新协议签署状态")
    public Wrapper<?> updateAgreement(@PathVariable("patientId") String patientId) throws Exception {
        WxOAuth2UserInfo userInfo = wxUserHolder.getUserInfo();
        sysNurseService.updateAgreement(patientId,userInfo.getOpenid(),userInfo.getNickname());
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE);
    }


    @PostMapping("/listPage")
    @ApiOperation(httpMethod = "POST", value = "分页查询申请入组的患者列表")
    public Wrapper<PageInfo<SysPatient>> listPageByOpenId(@ApiParam(name = "SysPatient", value = "SysPatient") @RequestBody PageParam<SysPatient> pageParam) {
        WxOAuth2UserInfo userInfo = wxUserHolder.getUserInfo();
        PageInfo<SysPatient> list = sysPatientService.listPageByOpenId(pageParam,userInfo.getOpenid(),Constants.NO);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, list);
    }


    @PostMapping("/nurseInput")
    @ApiOperation(httpMethod = "POST", value = "护士录入患者信息")
    public Wrapper<?> nurseInput(@ApiParam(name = "sysPatient", value = "sysPatient") @RequestBody @Validated(CreateValidate.class)  SysPatient sysPatient) {
        String result = sysPatientService.nurseInput(sysPatient);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE,result);
    }

    @PostMapping("/checkPatient")
    @ApiOperation(httpMethod = "POST", value = "护士确认患者信息")
    public Wrapper<?> checkPatient(@ApiParam(name = "sysPatient", value = "sysPatient") @RequestBody SysPatient sysPatient) throws Exception  {
        WxOAuth2UserInfo userInfo = wxUserHolder.getUserInfo();
        sysPatient.setUpdateBy(userInfo.getOpenid());
        sysPatient.setUpdater(userInfo.getNickname());
        sysPatientService.checkPatient(sysPatient);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE);
    }

    @PostMapping("/fistFollowPatient")
    @ApiOperation(httpMethod = "POST", value = "对患者进行首次随访")
    public Wrapper<?> fistFollowPatient(@ApiParam(name = "sysPatientFollow", value = "sysPatientFollow") @RequestBody @Validated(CreateValidate.class) SysPatientFollow sysPatientFollow) {
        WxOAuth2UserInfo userInfo = wxUserHolder.getUserInfo();
        sysPatientFollowService.fistFollowPatient(sysPatientFollow,userInfo.getOpenid(),userInfo.getNickname());
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE);
    }

}